package com.hongyuan.sellfitness.ui.home.view_mode;

import android.app.Application;

import androidx.annotation.NonNull;
import androidx.databinding.ObservableArrayList;
import androidx.databinding.ObservableList;

import com.hongyuan.mvvmhabitx.bus.RxBus;
import com.hongyuan.mvvmhabitx.bus.RxSubscriptions;
import com.hongyuan.sellfitness.BR;
import com.hongyuan.sellfitness.R;
import com.hongyuan.sellfitness.base.Constants;
import com.hongyuan.sellfitness.base.CustomViewModel;
import com.hongyuan.sellfitness.data.MyRepository;
import com.hongyuan.sellfitness.data.rxbus.ChangeStoreMessage;
import com.hongyuan.sellfitness.ui.home.beans.PriviteLessonBean;

import java.util.List;

import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;
import me.tatarka.bindingcollectionadapter2.ItemBinding;

public class PriviteLessonViwModel extends CustomViewModel<MyRepository> {

    private Disposable mSubscription;

    public PriviteLessonViwModel(@NonNull Application application, MyRepository model) {
        super(application, model);
    }

    //给RecyclerView添加ObservableList
    public ObservableList<PriviteLessonItemViewModel> observableList = new ObservableArrayList<>();
    //给RecyclerView添加ItemBinding
    public ItemBinding<PriviteLessonItemViewModel> itemBinding = ItemBinding.of(BR.viewModel, R.layout.item_privite_lesson);

    @Override
    public void onCreate() {
        super.onCreate();

        mSubscription = RxBus.getDefault().toObservable(ChangeStoreMessage.class)
                .observeOn(AndroidSchedulers.mainThread()) //回调到主线程更新UI
                .subscribe(progressLoadBean -> {
                    curPage = FIRST_PAGE;

                    //更新下数据
                    setParams("os_id",progressLoadBean.getOs_id());

                    requestData(Constants.GET_MY_COURSE_PRIVITE_LIST);
                });
        //将订阅者加入管理站
        RxSubscriptions.add(mSubscription);
    }


    @Override
    public void onDestroy() {
        super.onDestroy();

        //取消订阅，防止内存泄漏
        RxSubscriptions.remove(mSubscription);
    }

    @Override
    protected void returnData(int code, Object dataBean) {
        super.returnData(code, dataBean);

        if(code == Constants.GET_MY_COURSE_PRIVITE_LIST){

            List<PriviteLessonBean.ListBean> list = ((PriviteLessonBean)dataBean).getList();

            if(curPage == FIRST_PAGE){
                observableList.clear();
            }

            if(list != null && list.size() > 0){
                for(PriviteLessonBean.ListBean bean : list){
                    PriviteLessonItemViewModel itemViewModel = new PriviteLessonItemViewModel(this,bean);
                    observableList.add(itemViewModel);
                }
            }

            if(observableList.size() > 0){
                if(list == null || list.size() == 0){
                    getUC().getFinishLoadMoreData().call();
                }
            }else{

            }

        }
    }
}
